#!/usr/bin/env python3  /*设定脚本文件直接能执行*/
# -*- coding:utf-8 -*-   /*设置编码*/
"""
@Author: WANDERSON M.PIMENTA
@Software: PyCharm 、Qt Designer and PySide6
@File: tQTitleButton.py
@Time: 2024/05/11 16:35
@Function：ctQTitleButton：自定义标题栏按键
"""
from tQQtCore import *


# 自定义标题栏按键
class ctQTitleButton(QPushButton):
    """
    tQTitleButton：自定义标题栏按键
    """

    # 初始化
    def __init__(self,
                 parent,
                 app_parent=None,
                 tooltip_text="",
                 btn_id=None,
                 width=30,
                 height=30,
                 radius=8,
                 bg_color="#343b48",
                 bg_color_hover="#3c4454",
                 bg_color_pressed="#2c313c",
                 icon_color="#c3ccdf",
                 icon_color_hover="#dce1ec",
                 icon_color_pressed="#edf0f5",
                 icon_color_active="#f5f6f9",
                 icon_path="no_icon.svg",
                 dark_one="#1b1e23",
                 context_color="#568af2",
                 text_foreground="#8a95aa",
                 is_active=False
                 ):
        super().__init__()

        # 设置默认参数
        self.setFixedSize(width, height)
        self.setCursor(Qt.PointingHandCursor)
        self.setObjectName(btn_id)

        # 私有属性
        self._bg_color = bg_color
        self._bg_color_hover = bg_color_hover
        self._bg_color_pressed = bg_color_pressed
        self._icon_color = icon_color
        self._icon_color_hover = icon_color_hover
        self._icon_color_pressed = icon_color_pressed
        self._icon_color_active = icon_color_active
        self._context_color = context_color
        self._top_margin = self.height() + 6
        self._is_active = is_active
        # 参数设置
        self._set_bg_color = bg_color
        self._set_icon_path = icon_path
        self._set_icon_color = icon_color
        self._set_border_radius = radius
        # 父类
        self._parent = parent
        self._app_parent = app_parent

        # 工具提示
        self._tooltip_text = tooltip_text
        self._tooltip = _ToolTip(app_parent, tooltip_text, dark_one, context_color, text_foreground)
        self._tooltip.hide()

    # 按键设置激活函数
    def f_setActiveFunc(self, is_active):
        """
        function:  按键设置激活函数
              in:  is_active：True or False
             out:  None
          return:  None
          others:  Button Set Active Func
        """
        self._is_active = is_active
        self.repaint()

    # 获取按键是否激活函数
    def f_isActiveFunc(self):
        """
        function:  获取按键是否激活函数
              in:  None
             out:  None
          return:  Bool
          others:  Get Button Whether Active Func
        """
        return self._is_active

    # 重写绘制事件
    def paintEvent(self, event):
        # PAINTER
        paint = QPainter()
        paint.begin(self)
        paint.setRenderHint(QPainter.RenderHint.Antialiasing)
        if self._is_active:
            # BRUSH
            brush = QBrush(QColor(self._context_color))
        else:
            # BRUSH
            brush = QBrush(QColor(self._set_bg_color))
        # CREATE RECTANGLE
        rect = QRect(0, 0, self.width(), self.height())
        paint.setPen(Qt.NoPen)
        paint.setBrush(brush)
        paint.drawRoundedRect(
            rect,
            self._set_border_radius,
            self._set_border_radius
        )

        # DRAW ICONS
        self.__f_iconPaintFunc(paint, self._set_icon_path, rect)

        # END PAINTER
        paint.end()

    # 更改样式函数
    def __f_changeStyleFunc(self, event):
        if event == QEvent.Enter:
            self._set_bg_color = self._bg_color_hover
            self._set_icon_color = self._icon_color_hover
            self.repaint()
        elif event == QEvent.Leave:
            self._set_bg_color = self._bg_color
            self._set_icon_color = self._icon_color
            self.repaint()
        elif event == QEvent.MouseButtonPress:
            self._set_bg_color = self._bg_color_pressed
            self._set_icon_color = self._icon_color_pressed
            self.repaint()
        elif event == QEvent.MouseButtonRelease:
            self._set_bg_color = self._bg_color_hover
            self._set_icon_color = self._icon_color_hover
            self.repaint()

    # 重写鼠标进入按键事件
    def enterEvent(self, event):
        self.__f_changeStyleFunc(QEvent.Enter)
        self.__f_moveTooltip()
        # V1.0.0取消最大化、最小化、关闭按键的悬停提示功能
        self._tooltip.hide()
        # self._tooltip.show()

    # 重写鼠标离开按键事件
    def leaveEvent(self, event):
        self.__f_changeStyleFunc(QEvent.Leave)
        self.__f_moveTooltip()
        self._tooltip.hide()

    # 重写鼠标点击按键事件
    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.__f_changeStyleFunc(QEvent.MouseButtonPress)
            # SET FOCUS
            self.setFocus()
            # EMIT SIGNAL
            return self.clicked.emit()

    # 重写鼠标释放按键事件
    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.__f_changeStyleFunc(QEvent.MouseButtonRelease)
            # EMIT SIGNAL
            return self.released.emit()

    # 绘制图标函数
    def __f_iconPaintFunc(self, qp, image, rect):
        icon = QPixmap(image)
        painter = QPainter(icon)
        painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
        if self._is_active:
            painter.fillRect(icon.rect(), self._icon_color_active)
        else:
            painter.fillRect(icon.rect(), self._set_icon_color)
        qp.drawPixmap(
            (rect.width() - icon.width()) / 2,
            (rect.height() - icon.height()) / 2,
            icon
        )
        painter.end()

    # 按键设置图标
    def f_setIconFunc(self, icon_path):
        """
        function:  按键设置图标
              in:  icon_path：图标路径
             out:  None
          return:  None
          others:  Button Set Icon Func
        """
        self._set_icon_path = icon_path
        self.repaint()

    # 移除提示函数
    def __f_moveTooltip(self):
        # GET MAIN WINDOW PARENT
        gp = self.mapToGlobal(QPoint(0, 0))

        # SET WIDGET TO GET POSTION
        # Return absolute position of widget inside app
        pos = self._parent.mapFromGlobal(gp)

        # FORMAT POSITION
        # Adjust tooltip position with offset
        pos_x = (pos.x() - self._tooltip.width()) + self.width() + 5
        pos_y = pos.y() + self._top_margin

        # SET POSITION TO WIDGET
        # Move tooltip position
        self._tooltip.move(pos_x, pos_y)


# 自定义提示标签
class _ToolTip(QLabel):
    # 样式表
    style_tooltip = """ 
    QLabel {{		
        background-color: {_dark_one};	
        color: {_text_foreground};
        padding-left: 10px;
        padding-right: 10px;
        border-radius: 17px;
        border: 0px solid transparent;
        border-right: 3px solid {_context_color};
        font: 800 9pt "Segoe UI";
    }}
    """

    # 初始化
    def __init__(self, parent, tooltip, dark_one, context_color, text_foreground):
        QLabel.__init__(self)

        # 样式设置
        style = self.style_tooltip.format(_dark_one=dark_one, _context_color=context_color,
                                          _text_foreground=text_foreground)
        self.setObjectName(u"label_tooltip")
        self.setStyleSheet(style)
        self.setMinimumHeight(34)
        self.setParent(parent)
        self.setText(tooltip)
        self.adjustSize()

        # 设置投影
        self.shadow = QGraphicsDropShadowEffect(self)
        self.shadow.setBlurRadius(30)
        self.shadow.setXOffset(0)
        self.shadow.setYOffset(0)
        self.shadow.setColor(QColor(0, 0, 0, 80))
        self.setGraphicsEffect(self.shadow)
